<?php

//===============================================================================
// AlertPay Instant Payment Notification (IPN)
//===============================================================================
// Script: AlertURL.php by AlertPay_Peter
// Platform: PHP
// Developer Feedback:
// --------------
// If you have any questions about this script or any suggestions, please email to: devsupport@alertpay.com.


    // Security code variable
    $ap_SecurityCode;

    // Customer info variables
    $ap_CustFirstName;
    $ap_CustLastName;
	$ap_CustAddress;
	$ap_CustCity;
    $ap_CustCountry;
	$ap_CustZip;
	$ap_CustEmailAddress;

    // Common transaction variables
    $ap_ReferenceNumber;
	$ap_Status;
	$ap_PurchaseType;
	$ap_Merchant;
    $ap_ItemName;
	$ap_ItemCode;
	$ap_Description;
	$ap_Quantity;
	$ap_Amount;
	$ap_AdditionalCharges;
    $ap_ShippingCharges;
	$ap_TaxAmount;
	$ap_DiscountAmount;
	$ap_TotalAmount;
	$ap_Currency;
    $ap_Test;

    // Custom fields
    $ap_Apc_1;
	$ap_Apc_2;
	$ap_Apc_3;
	$ap_Apc_4;
	$ap_Apc_5;
	$ap_Apc_6;

    // Subscription variables
    $ap_SubscriptionReferenceNumber;
	$ap_TimeUnit;
	$ap_PeriodLength;
	$ap_PeriodCount;
	$ap_NextRunDate;
    $ap_TrialTimeUnit;
	$ap_TrialPeriodLength;
	$ap_TrialAmount;


	// Initialize variable
	setSecurityCodeVariable();

        if ($ap_SecurityCode != "W2cTGHv8v6ELEflpBndaQg")
        {
            // The Data is NOT sent by AlertPay.
            // Take appropriate action 
        }
        else
        {
            if ($ap_Test == "1")
            {
                // Your site is currently being integrated with AlertPay IPN for TESTING PURPOSES
                // ONLY. Don't store any information in your Production database and don't process
                // this transaction as a real order.
            }
            else
            {
                // Initialize variables
                setCustomerInfoVariables();
                setCommonTransactionVariables();

                // Initialize the custom field variables.
                setCustomFields();

                // If the transaction is subscription-based (recurring payment), initialize the
                // Subscription variables too.
                if ($ap_PurchaseType == "Subscription")
                {
                    setSubscriptionVariables();
                }

                if (strlen($ap_ReferenceNumber) == 0 && $ap_TrialAmount != "0")
                {
                    // Invalid reference number. The reference number is invalid because the ap_ReferenceNumber doesn't
                    // contain a value and the ap_TrialAmount is not equal to 0.
                }
                else
                {
                    if ($ap_Status == "Success")
                    {
                        // Transaction is complete. It means that the amount was paid successfully.
                        // Process the order here.

                        // Process non-subscription order.
                        if ($ap_PurchaseType != "Subscription")
                        {
                            // NOTE: The subscription variables are not applicable here. Don't use them.
                        }
                        // Process the subscription order. Use ap_SubscriptionReferenceNumber to uniquely identify
                        // this particular subscription transaction.
                        else
                        {
                            // Check whether the trial is free or not
                            if ($ap_TrialAmount == "0")
                            {
                                // Process the free trial here.
                                // NOTE: The ap_ReferenceNumber is always empty for trial periods and therefore you
                                // should not use it.
                            }
                            else
                            {
                                // The is not a free trial and ap_TrialAmount contains some amount and the
                                // ap_ReferenceNumber contains a valid transaction reference number.
                            }
                        }
                    }
                    else
                    {
                        // Transaction cancelled means seller explicitely cancelled the subscription or AlertPay 								
						// cancelled or it was cancelled since buyer didnt have enough money after resheduling after two times.
                        // Take Action appropriately
                    }
                }
            }
	}

	// Security code variable
	function setSecurityCodeVariable()
	{
        global $ap_SecurityCode = $_POST['ap_securitycode'];
	}
	
	// Customer info variables
    function setCustomerInfoVariables()
    {
        global $ap_CustFirstName =$_POST['ap_custfirstname'];
        global $ap_CustLastName = $_POST['ap_custlastname'];
        global $ap_CustAddress = $_POST['ap_custaddress'];
        global $ap_CustCity = $_POST['ap_custcity'];
        global $ap_CustCountry = $_POST['ap_custcountry'];
        global $ap_CustZip = $_POST['ap_custzip'];
        global $ap_CustEmailAddress = $_POST['ap_custemailaddress'];
        global $ap_PurchaseType = $_POST['ap_purchasetype'];
        global $ap_Merchant = $_POST['ap_merchant'];
    }
	
	// Common transaction variables
    function setCommonTransactionVariables()
    {
        global $ap_ItemName = $_POST['ap_itemname'];
        global $ap_Description = $_POST['ap_description'];
        global $ap_Quantity = $_POST['ap_quantity'];
        global $ap_Amount = $_POST['ap_amount'];
        global $ap_AdditionalCharges=$_POST['ap_additionalcharges'];
        global $ap_ShippingCharges=$_POST['ap_shippingcharges'];
        global $ap_TaxAmount=$_POST['ap_taxamount'];
        global $ap_DiscountAmount=$_POST['ap_discountamount'];
        global $ap_TotalAmount = $_POST['ap_totalamount'];
        global $ap_Currency = $_POST['ap_currency'];
        global $ap_ReferenceNumber = $_POST['ap_referencenumber'];
        global $ap_Status = $_POST['ap_status'];
        global $ap_ItemCode = $_POST['ap_itemcode'];
        global $ap_Test = $_POST['ap_test'];
    }
	
	// Subscription variables
    function setSubscriptionVariables()
    {
	    global $ap_SubscriptionReferenceNumber = $_POST['ap_subscriptionreferencenumber'];
	    global $ap_TimeUnit = $_POST['ap_timeunit'];
	    global $ap_PeriodLength=$_POST['ap_periodlength'];
	    global $ap_PeriodCount=$_POST['ap_periodcount'];
	    global $ap_NextRunDate=$_POST['ap_nextrundate'];
	    global $ap_TrialTimeUnit=$_POST['ap_trialtimeunit'];
	    global $ap_TrialPeriodLength=$_POST['ap_trialperiodlength'];
	    global $ap_TrialAmount=$_POST['ap_trialamount'];
    }

	// Custom fields
    function setCustomFields()
    {
        global $ap_Apc_1 = $_POST['apc_1'];
        global $ap_Apc_2 = $_POST['apc_2'];
        global $ap_Apc_3 = $_POST['apc_3'];
        global $ap_Apc_4 = $_POST['apc_4'];
        global $ap_Apc_5 = $_POST['apc_5'];
        global $ap_Apc_6 = $_POST['apc_6'];
    }

?>
